{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c8ba2bcb-830f-4a05-b5fc-e16b60b92050",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "330b741c-6eab-45e6-ab30-ec7cd56d5b38",
   "metadata": {},
   "source": [
    "# 矩阵格式（多维）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16d6d809-cc65-4c40-a4a1-9b64396ce6f1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.array([[1,2,3],\n",
    "                     [4,5,6],\n",
    "                     [7,8,9]])\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "78d2de2d-8eaf-4bd9-a472-3fe77238be18",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 3)\n",
      "9\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "print(fang_array.shape)\n",
    "print(fang_array.size)\n",
    "print(fang_array.ndim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ea7dfcf5-4886-4ff3-98e0-cd4059a227ba",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[1,1] #索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e26b2fea-c1a4-4dfe-b5e1-b22575931b10",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4, 10,  6],\n",
       "       [ 7,  8,  9]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[1,1] = 10\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "27265c78-bfe6-43fb-9285-e4a829904dad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10,  6])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[1] #索引行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "23dd002b-a04d-4d75-a90d-cd567a4780fb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 10,  8])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[:,1] #索引列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "77d5aae7-0dbc-4b74-8150-30ad14a9c7ae",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[1,0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3469358a-787f-4145-9f1a-868362808290",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4, 10,  6],\n",
       "       [ 7,  8,  9]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array2 = fang_array\n",
    "fang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "60ace0c4-f9b0-402a-8b84-a0246a90076d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fang_array2[1,1] = 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f2f8f642-6469-4744-95f4-ecd3c2987fe4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array #修改array2,array也被修改\n",
    "#由此可以判断，array2和array指向的是同一个地址\n",
    "#array2没有开辟新内存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "336253e4-bb75-4649-9338-6a768527f92f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array2 = fang_array.copy()\n",
    "fang_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c3107cdb-dd7b-4047-862a-903cf3f96e6b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array2[1,1] = 10000\n",
    "fang_array #使用.copy()后，会新分配内存,修改不会关联"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f2e85a55-57d1-43fe-8539-d81374ea7684",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.arange(0,100,10)\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9b072347-cb32-46a9-ab9d-d7186cc7c82e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False,  True,  True, False,  True,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = np.array([0,0,1,0,1,1,0,1,1,1], dtype=bool)\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "f4b7709c-0ac6-4c2a-93f8-8d22bb27bf68",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, 40, 50, 70, 80, 90])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[mask] #根据布尔值索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "274bb757-50bf-4bfb-b2aa-e06f168dde3d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.92350444, 0.58380924, 0.10319051, 0.55952273, 0.71165423,\n",
       "       0.54056046, 0.98631418, 0.22363543, 0.49944495, 0.98041532])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array = np.random.rand(10)\n",
    "random_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "960bfb53-ae00-48a6-a172-2498879cd977",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False,  True,  True,  True,  True, False, False,\n",
       "        True])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = random_array > 0.5\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "45a90d14-37ea-46e2-995c-c856d43a21a3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.92350444, 0.58380924, 0.55952273, 0.71165423, 0.54056046,\n",
       "       0.98631418, 0.98041532])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array[mask]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "d423fe54-f80f-4d41-90be-de2af2d67908",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.92350444, 0.58380924, 0.55952273, 0.71165423, 0.54056046,\n",
       "       0.98631418, 0.98041532])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random_array[random_array > 0.5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "f84489a2-3aeb-4e7e-9cfb-fa7c76242bdb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.array([10,20,30,40,50])\n",
    "fang_array > 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e2f90c16-ca71-4cc2-b647-a47bf8d8a9d1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([3, 4]),)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(fang_array > 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "11531a45-f506-4f86-a11c-9eafdbf6ab11",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([40, 50])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array[np.where(fang_array > 30)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce58ff41-9214-4e20-8fdf-c7ecf2d03909",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
